program Project;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  PElement = ^Element;

  Element = record
    value : real;
    power: real;
    next : PElement;
  end;



var
  Head ,head1,head12: PElement;



function NewElement(value, power : real) : PElement;
var p : PElement;
begin
  New(p);
  p^.value := value;
  p^.power := power;
  p^.next := nil;
  NewElement := p;
end;


procedure AddElement(var h : PElement; e : PElement);
begin
  e^.next := h;
  h := e;
end;


procedure PrintList(h : PElement);
begin
  while h <> nil do
  begin
    Write(h^.value : 0:0, '*x^', h^.power : 0:0, ' + ');
    h := h^.next;
  end;
end;






procedure deleteoddelement (h:pelement);
var p,x:pelement;
dod:integer;
begin
  if (head=nil) then
    exit;
  if (h^.next=nil) then
    exit;

   x:=h;
   dod:=1;
   while  (x<>nil) do
   begin

     x^.value:=x^.value+dod;
     dod:=dod+2;
     p:=x^.next;
     if (p=nil) then
      break;
     p:=p^.next;
     dispose(x^.next);
     x^.next:=p;
     x:=x^.next;
   end;

end;



function massplus ( head, head1 :pelement) :pelement ;
var
 f,h,tail,head12, p:pelement;
 begin
   head12:=nil;

   f:=head;
   while (f<>nil) do
    begin

    tail:=head1;
    while (tail<>nil) do
     begin
      if (tail^.power=f^.power) then
        break;
      tail:=tail^.next;
     end;

       if (tail=nil) then
        addElement(head1,newelement(0, f^.power));
       f:=f^.next;
  end;
                   

   tail:=head1;
   while (tail<>nil) do
    begin

    f:=head;
    while (f<>nil) do
     begin
      if (tail^.power=f^.power) then
        break;
      f:=f^.next;
     end;

    p := NewElement(tail^.value,tail^.power);
    if (f<>nil) then
     begin
      p^.value := f^.value+p^.value;
     end;
     addelement(head12,p);

    tail:=tail^.next;
    end;

   massplus :=head12;

end;



begin
  { TODO -oUser -cConsole Main : Insert code here }

  Head := nil;
  Head1 := nil;

  AddElement(Head, NewElement(1,3));
  AddElement(Head, NewElement(3,1));

  AddElement(Head1, NewElement(1,2));
  AddElement(Head1, NewElement(2,1));
  AddElement(Head1, NewElement(1,0));

  PrintList(Head);
  writeln;
  PrintList(Head1);
  writeln;

   Head12 := massplus(head,head1);

  PrintList(Head12);
  writeln;

  ReadLn;
end.
